package com.piotrs10.bsk.gui.views;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window.Notification;

public class LoginView extends VerticalLayout implements ClickListener {

	/**
	 * Constants
	 */
	private static final long serialVersionUID = 9159972758516359149L;
	private final String LOGIN_CAPTION = "Login";
	private final String PASSWORD_CAPTION = "Haslo";
	private final String LOGIN_BUTTON_CAPTION = "Zaloguj";
	private final String WRONG_CREDENTIALS = "Nieprawidlowy login lub haslo!";
	/**
	 * Fields
	 */
	private Button loginButton;
	private TextField loginField;
	private PasswordField passwordField;

	/**
	 * Methods
	 */

	public LoginView() {
		super();
		this.setMargin(true);
		this.loginField = new TextField(this.LOGIN_CAPTION);
		this.passwordField = new PasswordField(this.PASSWORD_CAPTION);
		this.loginButton = new Button(this.LOGIN_BUTTON_CAPTION);
		this.loginButton.addListener(this);
		this.addComponent(this.loginField);
		this.addComponent(this.passwordField);
		this.addComponent(this.loginButton);
	}

	@Override
	public void buttonClick(ClickEvent event) {
		boolean loginResult = this.performLogin(
				(String) this.loginField.getValue(),
				(String) this.passwordField.getValue());
		if (loginResult == true) {
			this.getWindow().setContent(new MainView());
		} else {
			this.getWindow().showNotification(this.WRONG_CREDENTIALS,
					Notification.TYPE_ERROR_MESSAGE);
		}
	}

	private boolean performLogin(String caption, String password) {
		// TODO: Zrobic prawdziwe logowanie
		if (caption.equals("aaa") && password.equals("aaa")) {
			return true;
		} else {
			return false;
		}
	}

}
